// mircoApp/competitionApply/myResult/myResult.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    query:{
      keyword:'',
      page:1,
      id:''
    },
    resultArr:[],
    totalPage:0,
    isLoadMore: true,
    applyId:'',

    // 海报按钮
    posterShow: false,
    posterImg: null,
    isFrontCanvas: false, //是否前端控制生成海报，false为后端生成
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id) {
      this.setData({
        'query.id':Number(options.id)
      })
    }
    this.goodsList();
  },
  onSearch(event) {
    console.log(event);
    this.setData({
      'query.keyword':event.detail,
      'query.page':1,
      isLoadMore:true,
      resultArr:[]
    })
    this.goodsList();
  },
  onClear(event) {
    console.log(event);
    this.setData({
      'query.keyword':'',
      'query.page':1,
      isLoadMore:true,
      resultArr:[]
    })
    this.goodsList();
  },
  examineClick(event) {
    console.log(event);
    let {item} = event.detail;
    let urlStr = app.getNetAddresss("plugin.event-registration.api.transcript.view-certificate");
    app._getNetWork({
      url: urlStr,
      data: {apply_id:item.id},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          console.log(data);
          if(data.type == 2) {
            this.setData({
              posterImg:data.url,
              posterShow:true
            })
          } else {
            if(data.url) { //有时候会返回空
              wx.navigateTo({
                url: '/mircoApp/competitionApply/out/out?src='+ data.url,
              })
            }
            
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  CloseBtn() {
    this.setData({
      posterShow:false
    })
  },
  saveImg() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.writePhotosAlbum"]) {
          wx.authorize({
            scope: "scope.writePhotosAlbum",
            success() {
              console.log("授权成功");
            },
          });
        }
      },
    });
    var imgSrc = this.data.minImga;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限成功,再次长按保存图片",
                      duration: 1500,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限失败,保存图片需要用户授权",
                      duration: 1500,
                    });
                  }
                },
              });
            }
          },
          complete(res) {
            console.log(res);
          },
        });
      },
    });
  },
   //海报生成
  //  postShow(e) {
  //   this.setData({
  //     posterShow: true,
  //     showSahre: false,
  //   });
  // },
  posterClick() {
    this.setData({
      posterShow: true,
    });
  },
  posterClose() {
    this.setData({
      posterShow: false,
    });
  },
  goodsList() {
    let urlStr = app.getNetAddresss("plugin.event-registration.api.transcript.getList");
    app._postNetWork({
      url: urlStr,
      data:this.data.query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          console.log(data);
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            resultArr: data.data,
          })
          console.log(this.data.resultArr);
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let urlStr = app.getNetAddresss("plugin.event-registration.api.transcript.getList");
      app._postNetWork({
        url: urlStr,
        data: this.data.query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.list?.data;
          console.log(list);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              resultArr : this.data.resultArr.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
     title: '刷新中...',
   })
   this.setData({
     'query.page':1,
     resultArr:[],
     isLoadMore: true,
   })
   this.goodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})